<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>添加巡视记录页</title>
		<link href="../../css/mui.min.css" rel="stylesheet"/>
		<link rel="stylesheet" href="../../css/CFNetPP_Base.css">
		<link rel="stylesheet" type="text/css" href="../../css/base.css"/>
		<!--	<link rel="stylesheet" type="text/css" href="../../css/mui.min.css">-->
		<style>
			.imgDiv {
				position: absolute;
				padding-left: 5px;
				height: 240px;
				background-color: #ffffff;
				width: 100%;
			}
			/*
			 * 删除图片
			 */
			.deleteImg {
				position: absolute;
				margin-top: -0px;
				margin-left: -16px;
				width: 24px;
				height: 24px;
			}
			/*多张图片*/
			#ul1 {
				width: 100%;
				position: relative;
				margin: 10px auto;
			}
			#ul1 li {
				width: 60px;
				height: 50px;
				float: left;
				list-style: none;
				margin: 10px;
			}
			.left_px {
				margin-left: 5px;
			}
			.line_heigt_px {
				line-height: 50px;
			}
			.span_wight {
				width: 54%;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		</style>
	</head>
	<body  >
		<div class="base_style_color" id='BarAppearance' style="height: 22px;width: 100%; position:fixed; left: 0px;top:0px;"></div>
		<div class="base_navigation_bar base_style_color base_navigation_item_bar" style="position: fixed;left: 0px;top: 20px;">
			<a class="base_left_item" href="#" onclick="backClick()"> <img class="base_back_icon" src="../../image/icon_jiantou.png" alt=""> </a>
			<span>新巡视记录</span>
			<a class="base_right_item" href="#" onclick="addpatrol()"> 提交 </a>
		</div>
		<div class="newcheckrecord " style="padding-top: 70px">
			<form class="mui-input-group font14 base_dark_grey">
				<div class="mui-input-row" onClick="TrainSelectCompany()">
					<label>巡视单位:</label>
					<span class="stuname mui-navigate-right span_wight " id="trainelectcompany_id"></span>
				</div>
				<div class="mui-input-row" onClick="changeState()">
					<label>巡视学生:</label>
					<span class="stuname mui-navigate-right "  id="changestate"></span>
				</div>
				<div class="mui-input-row">
					<label>巡视形式:</label>
					<div class="dropdown" id="dropdownbtn">
						<span id="className" class="className">实地考察</span>
						<span class="sanjiao"><img src="../../image/icon_sanjiao.png" alt=""></span>
					</div>
				</div>
				<div class="mui-input-row line_heigt_px">
					<label>巡视时间:</label>
					<span  class="mui-input-clear"  id="pator_time"></span>
				</div>
				<div id="dropdownlist" class=" droplist base_grey font14">
					<ul class="mui-table-view">
						<li class="mui-table-view-cell" index="1">
							<a href="#">实地考察</a>
						</li>
						<li class="mui-table-view-cell" index="2">
							<a href="#">电话回访</a>
						</li>
						<li class="mui-table-view-cell" index="3">
							<a href="#">网络回访</a>
						</li>
						<li class="mui-table-view-cell" index="4">
							<a href="#">其他</a>
						</li>
					</ul>
				</div>
			</form>
			<div class="iptreason top_15" >
				<label class="shuoming ">巡视报告：</label>
				<div  style=" height: 110px;">
					<textarea id="problem_id" class="horcenter font14 base_grey   Word_wrap"  placeholder="巡视遇到的问题......"></textarea>
				</div>
				<div class="imgDiv " id="imgDiv">
					<ul id="ul1" class="imagelist"></ul>
				</div>
			</div>
			<div id="picture" class="mui-popover mui-popover-action mui-popover-bottom">
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						<a href="#" typeid="0" style="color:  #222222">拍照</a>
					</li>
					<li class="mui-table-view-cell">
						<a href="#" typeid="1" style="color:  #222222">从相册选取</a>
					</li>
				</ul>
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						<a href="#picture" typeid="2" style="color:  #222222"><b>取消</b></a>
					</li>
				</ul>
			</div>
		</div>
		<script src="../../script/mui.min.js"></script>
		<script src="../../script/common.js"></script>
		<script type="text/javascript" src="../../script/processing_images.js"></script>
		<script type="text/javascript" src="../../script/api.js"></script>
		<script src="../../script/jquery.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="../../script/requestTool.js"></script>
		<script>
			var form_text = "1";
			var imageArr = new Array();
			//原路径图片路径
			var compressArr = new Array();
			//压缩路径图片路径
			var picurls = [];
			//上传后百度云的图片路径
			var cid;
			//企业id
			var time;
			var stuid = "";
			//学生id串
			$(function() {
				$("#dropdownbtn").click(function() {
					$('#dropdownlist').toggleClass("show");
				})
				// 点击出下拉菜单
				$("#dropdownlist .mui-table-view-cell").click(function() {
					$('#dropdownlist').toggleClass("show");
					var form = $(this).text();
					$("#className").text(form);
					form_text = $(this).attr('index');
				});
				// 点击显示选中的文字
			})
			apiready = function() {
				SendBehaviorTrajectoriesNotifi1('0', '添加巡视记录页', '116', api.pageParam.sourceurl, api.pageParam.sourcetitle, window.location.pathname, document.title);
				listimg();
				//接收到企业
				api.addEventListener({
					name : 'selectCompanyNotifi'
				}, function(ret, err) {
					if (ret.value) {
						var cname = ret.value.companyname;
						cid = ret.value.id;
						$('#trainelectcompany_id').text(cname);
						stuid = "";
						$("#changestate").text("");
					}
				});
				//接收学生
				api.addEventListener({
					name : 'selectStudentNotifi'
				}, function(ret, err) {
					if (ret.value) {
						time = getCurrentDate((Date.parse(new Date())) / 1000);
						$('#pator_time').text(time);
						stuid = "";
						var stuarr = ret.value.stuArr;
						var len = stuarr.length;
						var studentnam = "";
						if (len > 1) {
							studentnam = stuarr[0].studentname + "," + stuarr[1].studentname + "等" + len + "人";
						} else {
							studentnam = stuarr[0].studentname + len + "人";
						}
						for (var i = 0; i < len; i++) {
							var sid = stuarr[i].studentid;
							stuid = stuid + "," + sid;
						}
						$("#changestate").text(studentnam);
					}
				});
			}
			function backClick() {
				//				SendBehaviorTrajectoriesNotifi('1', '添加巡视记录页', '116');
				SendBehaviorTrajectoriesNotifi1('1', '添加巡视记录页', '116', api.pageParam.sourceurl, api.pageParam.sourcetitle, window.location.pathname, document.title);
				api.closeWin();
			}

			/**
			 *添加图片
			 *  */
			function listimg() {
				var num = imageArr.length;
				var imageHtmls = '';
				if (num > 0 && num < 10) {
					for ( i = 0; i < num; i++) {
						var imageHtml = '<li>' + '<img class="selectImg" id=' + num + ' src=' + imageArr[i] + ' width="60"   height="60" />' + '<img class="deleteImg" id=' + num + ' src="../../image/del_pic.png" onClick="deleteImg(' + i + ')">' + '</li>';
						imageHtmls = imageHtmls + imageHtml;
					}
				}
				if (num < 9) {
					var seleimg = '<li>' + '<img class="selectImg"    src="../../image/add_pic.png" width="60"  onclick="setupActionSheet()" />' + '</li>';
					imageHtmls = imageHtmls + seleimg;
				}
				$("#ul1").html(imageHtmls);
			}

			/**
			 * *删除删除图片*/
			function deleteImg(i) {
				compressArr.splice(i, 1);
				imageArr.splice(i, 1);
				listimg();
			}

			//弹出拍照的底部框
			function setupActionSheet() {
				mui('#picture').popover('toggle');
				mui('body').on('tap', '.mui-popover-action li>a', function() {
					var typeid = $(this).attr('typeid');
					if (typeid == 0) {//拍照
						mui('#picture').popover('toggle');
						api.getPicture({
							sourceType : 'camera',
							encodingType : 'png',
							mediaValue : 'pic',
							destinationType : 'url',
							allowEdit : true,
							quality : 50,
							saveToPhotoAlbum : false
						}, function(ret, err) {
							if ($('#picture').is(':visible')) {
								$('#picture').hide();
							}
							if (ret) {
								var photoPath = ret.data;
								if (photoPath != '') {
									imageArr.push(photoPath);
									listimg();
									//	                         imgCompress(photoPath, 0.5, 0.5, function(compressImg) {
									//	                          alert("压缩图片之后路径++"+compressImg);
									//                             compressArr.push(compressImg);
									//
									//                          });
								}
							} else {
							}
						});
					} else if (typeid == 1) {//从相册选择
						mui('#picture').popover('toggle');
						api.getPicture({
							sourceType : 'album',
							encodingType : 'png',
							mediaValue : 'pic',
							destinationType : 'url',
							allowEdit : true,
							quality : 50,
							saveToPhotoAlbum : false
						}, function(ret, err) {
							if ($('#picture').is(':visible')) {
								$('#picture').hide();
							}
							if (ret) {
								var photoPath = ret.data;
								if (photoPath != '') {
									imageArr.push(photoPath);
									listimg();
									//				    	        imgCompress(photoPath, 0.5, 0.5,  function(compressImg) {
									//				    	          alert("压缩图片之后路径++"+compressImg);
									//                                compressArr.push(compressImg);
									//
									//                          });
								}
							} else {
							}
						});
					}
				});
			}

			//上传图片
			function addpatrol() {
				var company = $("#trainelectcompany_id").text();
				var report = document.getElementById("problem_id").value;
				var student = $("#changestate").text();
				if (company == '') {
					api.toast({
						msg : '企业名称不能为空'
					});
					return;
				}
				if (student == '') {
					api.toast({
						msg : '学生不能为空'
					});
					return;
				}
				if (report == '') {
					api.toast({
						msg : '报告不能为空'
					});
					return;
				}
				$('.base_right_item').hide();
				api.showProgress({
					style : 'default',
					animationType : 'fade',
					title : '正在上传...',
					text : '请稍候...',
					modal : false
				});
				var num = imageArr.length;
				if (num > 0) {//有图片
					for (var i = 0; i < num; i++) {
						imgCompress(imageArr[i], 0.5, 0.5, function(compressImg) {
							UploadFile_ali(Upload_File_img, compressImg, function(ret) {
								//							uploadFile(compressImg, function(serverImg) {
								picurls.push(ret.fileurl);
								var k = picurls.length;
								if (k == num) {
									var str = picurls.join(",");
									Upload_data(report, str);
								}
							});
						});
					}
				} else {//无图片
					Upload_data(report, "");
				}
			}

			function loaddata() {
				var url = urlApplst;
				var params = {
					uid : $api.getStorage('cfnetppuid'),
					jid : $api.getStorage('cfnetppjxid'),
				};
				// alert(JSON.stringify(params));
				cfnetppPOST(url, params, true, function(data, status) {
					hideProgress();
					if (status == 'success') {
						var jsonData = data.json;
						$api.setStorage('cfnetUserName', jsonData.username);
					}
				});
			}

			/**
			 *上传数据
			 *   Uid：用户uid
			 school_id :  学校id
			 report  :  总结报告内容
			 type : 微博类型 1.图片，0：无图片
			 type_data :  图片上传后的返回地址
			 studentid：巡视学生id（例如：2025,2028）
			 time  ：巡视时间
			 corpname  ：企业名称
			 form   ：巡视形式(1：实地考察2：电话回访3：网络回访4：网络回访5：其他)
			 *  */
			function Upload_data(report, imageurl) {
				//			SendBehaviorTrajectoriesNotifi('1', '添加巡视记录页', '116');
				params = {
					uid : $api.getStorage('cfnetppuid'),
					school_id : $api.getStorage('cfnetppjid'),
					report : report,
					imageurl : imageurl,
					studentid : stuid.substring(1, stuid.length),
					corpid : cid,
					form : form_text,
					message : $api.getStorage('cfnetUserName') + '的巡视记录'
				};
				cfnetppPOST(urlAddpatro, params, false, function(data, status) {
					api.hideProgress();
					SendBusinessOperationsLogNotifi('添加巡视记录页', '116', params, params, '', data, '发布新巡检记录接口', '116-01')
					if (status == 'success') {
						if (data.status == 1) {
							api.sendEvent({
								name : 'addpatroNotifi'
							});
							api.toast({
								msg : '添加成功'
							});
							SendBehaviorTrajectoriesNotifi1('1', '添加巡视记录页', '116', api.pageParam.sourceurl, api.pageParam.sourcetitle, window.location.pathname, document.title);
							api.closeWin();
						} else {
							$('.base_right_item').show();
							api.toast({
								msg : '添加失败'
							});
						}
					} else {
						$('.base_right_item').show();
						api.toast({
							msg : '添加失败'
						});
					}
				});
			}

			/**
			 *巡视企业
			 *   */
			function TrainSelectCompany() {
				//	 	alert("*巡视企业");
				api.openWin({useWKWebView:true,
					name : 'TrainSelectCompany',
					url : 'TrainSelectCompany.html',
				});
			}

			/**
			 *巡视学生
			 *   */
			function changeState() {
				var trainSelectcompany = document.getElementById("trainelectcompany_id").innerText;
				if (trainSelectcompany.length > 0) {
					api.openWin({useWKWebView:true,
						name : 'Teacher_select_student_window',
						url : 'Teacher_select_student_window.html',
						pageParam : {
							cid : cid, //企业id
							type : 1
						}
					});
				} else {
					api.toast({
						msg : '请添加企业后,在选择学生'
					});
				}
			}
		</script>
	</body>
</html>
